<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 图书管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
            width: 100%;
            max-width: 900px;
            display: flex;
            min-height: 600px;
        }

        .login-left {
            flex: 1;
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            padding: 60px 40px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            text-align: center;
            color: white;
            position: relative;
            overflow: hidden;
        }

        .login-left::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="white" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="white" opacity="0.1"/><circle cx="50" cy="10" r="0.5" fill="white" opacity="0.1"/><circle cx="10" cy="60" r="0.5" fill="white" opacity="0.1"/><circle cx="90" cy="40" r="0.5" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
            opacity: 0.3;
        }

        .login-left-content {
            position: relative;
            z-index: 1;
        }

        .login-logo {
            font-size: 48px;
            margin-bottom: 20px;
            color: #3498db;
        }

        .login-title {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 15px;
        }

        .login-subtitle {
            font-size: 16px;
            opacity: 0.9;
            margin-bottom: 30px;
            line-height: 1.6;
        }

        .login-features {
            list-style: none;
            text-align: left;
        }

        .login-features li {
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            font-size: 14px;
        }

        .login-features i {
            margin-right: 10px;
            color: #3498db;
            width: 20px;
        }

        .login-right {
            flex: 1;
            padding: 60px 40px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .login-form-header {
            text-align: center;
            margin-bottom: 40px;
        }

        .login-form-title {
            font-size: 28px;
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .login-form-subtitle {
            color: #7f8c8d;
            font-size: 16px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #495057;
            font-weight: 500;
            font-size: 14px;
        }

        .form-control {
            width: 100%;
            padding: 15px 20px;
            border: 2px solid #e9ecef;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-control:focus {
            outline: none;
            border-color: #3498db;
            background: white;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
        }

        .input-group {
            position: relative;
        }

        .input-group i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #6c757d;
            font-size: 18px;
        }

        .input-group .form-control {
            padding-left: 50px;
        }

        .btn {
            width: 100%;
            padding: 15px 20px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(52, 152, 219, 0.3);
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
        }

        .login-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 20px 0;
            font-size: 14px;
        }

        .remember-me {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .remember-me input[type="checkbox"] {
            width: 16px;
            height: 16px;
            accent-color: #3498db;
        }

        .forgot-password {
            color: #3498db;
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .forgot-password:hover {
            color: #2980b9;
        }

        .login-divider {
            text-align: center;
            margin: 30px 0;
            position: relative;
        }

        .login-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: #e9ecef;
        }

        .login-divider span {
            background: white;
            padding: 0 20px;
            color: #6c757d;
            font-size: 14px;
        }

        .register-link {
            text-align: center;
            margin-top: 20px;
            font-size: 14px;
            color: #6c757d;
        }

        .register-link a {
            color: #3498db;
            text-decoration: none;
            font-weight: 600;
        }

        .register-link a:hover {
            color: #2980b9;
        }

        .back-home {
            position: absolute;
            top: 20px;
            left: 20px;
            color: white;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
            z-index: 2;
        }

        .back-home:hover {
            color: #3498db;
            transform: translateX(-5px);
        }

        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #f5c6cb;
            display: none;
        }

        .success-message {
            background: #d4edda;
            color: #155724;
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #c3e6cb;
            display: none;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .login-container {
                flex-direction: column;
                max-width: 400px;
            }

            .login-left {
                padding: 40px 20px;
                min-height: 300px;
            }

            .login-right {
                padding: 40px 20px;
            }

            .login-title {
                font-size: 24px;
            }

            .login-form-title {
                font-size: 24px;
            }
        }

        /* 动画效果 */
        .fade-in {
            animation: fadeIn 0.6s ease-in;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .slide-in-left {
            animation: slideInLeft 0.6s ease-out;
        }

        @keyframes slideInLeft {
            from { opacity: 0; transform: translateX(-50px); }
            to { opacity: 1; transform: translateX(0); }
        }

        .slide-in-right {
            animation: slideInRight 0.6s ease-out;
        }

        @keyframes slideInRight {
            from { opacity: 0; transform: translateX(50px); }
            to { opacity: 1; transform: translateX(0); }
        }
    </style>
</head>
<body>
    <a href="index.html" class="back-home">
        <i class="fas fa-arrow-left"></i>
        返回首页
    </a>

    <div class="login-container">
        <div class="login-left slide-in-left">
            <div class="login-left-content">
                <div class="login-logo">
                    <i class="fas fa-book"></i>
                </div>
                <h1 class="login-title">图书管理系统</h1>
                <p class="login-subtitle">
                    专业的图书管理解决方案<br>
                    让图书管理更简单、更高效
                </p>
                <ul class="login-features">
                    <li>
                        <i class="fas fa-check-circle"></i>
                        完整的图书信息管理
                    </li>
                    <li>
                        <i class="fas fa-check-circle"></i>
                        智能的借阅管理系统
                    </li>
                    <li>
                        <i class="fas fa-check-circle"></i>
                        用户权限分级控制
                    </li>
                    <li>
                        <i class="fas fa-check-circle"></i>
                        数据统计与分析
                    </li>
                    <li>
                        <i class="fas fa-check-circle"></i>
                        安全可靠的数据保护
                    </li>
                </ul>
            </div>
        </div>

        <div class="login-right slide-in-right">
            <div class="login-form-header">
                <h2 class="login-form-title">欢迎登录</h2>
                <p class="login-form-subtitle">请输入您的账号信息</p>
            </div>

            <div class="error-message" id="errorMessage"></div>
            <div class="success-message" id="successMessage"></div>

            <form id="loginForm" class="fade-in">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <div class="input-group">
                        <i class="fas fa-user"></i>
                        <input type="text" id="username" name="username" class="form-control" placeholder="请输入用户名" required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="password">密码</label>
                    <div class="input-group">
                        <i class="fas fa-lock"></i>
                        <input type="password" id="password" name="password" class="form-control" placeholder="请输入密码" required>
                    </div>
                </div>

                <div class="login-options">
                    <label class="remember-me">
                        <input type="checkbox" id="rememberMe" name="rememberMe">
                        <span>记住我</span>
                    </label>
                    <a href="#" class="forgot-password">忘记密码？</a>
                </div>

                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-sign-in-alt"></i>
                    登录
                </button>

                <div class="login-divider">
                    <span>或者</span>
                </div>

                <button type="button" class="btn btn-secondary" id="registerBtn">
                    <i class="fas fa-user-plus"></i>
                    注册新账号
                </button>

                <div class="register-link">
                    还没有账号？ <a href="#" id="registerLink">立即注册</a>
                </div>
            </form>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            console.log('登录页面加载完成');

            // 登录表单提交
            $('#loginForm').on('submit', function(e) {
                e.preventDefault();
                console.log('开始登录...');

                var username = $('#username').val().trim();
                var password = $('#password').val().trim();

                if (!username || !password) {
                    showError('请输入用户名和密码');
                    return;
                }

                // 显示加载状态
                var $submitBtn = $(this).find('button[type="submit"]');
                var originalText = $submitBtn.html();
                $submitBtn.html('<i class="fas fa-spinner fa-spin"></i> 登录中...').prop('disabled', true);

                // 发送登录请求
                $.ajax({
                    url: '/user/login',
                    method: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        username: username,
                        password: password
                    }),
                    success: function(res) {
                        console.log('登录响应:', res);
                        
                        if (res.code === 200) {
                            showSuccess('登录成功，正在跳转...');
                            
                            // 保存用户信息到localStorage
                            if (res.data) {
                                localStorage.setItem('userInfo', JSON.stringify(res.data));
                                console.log('用户信息已保存:', res.data);
                            }
                            
                            // 延迟跳转
                            setTimeout(function() {
                                window.location.href = 'dashboard.html';
                            }, 1000);
                        } else {
                            showError(res.message || '登录失败，请检查用户名和密码');
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('登录请求失败:', error);
                        showError('网络错误，请稍后重试');
                    },
                    complete: function() {
                        // 恢复按钮状态
                        $submitBtn.html(originalText).prop('disabled', false);
                    }
                });
            });

            // 注册按钮点击
            $('#registerBtn, #registerLink').on('click', function(e) {
                e.preventDefault();
                showRegisterDialog();
            });

            // 显示错误信息
            function showError(message) {
                $('#errorMessage').text(message).show();
                $('#successMessage').hide();
                
                // 3秒后自动隐藏
                setTimeout(function() {
                    $('#errorMessage').hide();
                }, 3000);
            }

            // 显示成功信息
            function showSuccess(message) {
                $('#successMessage').text(message).show();
                $('#errorMessage').hide();
            }

            // 显示注册对话框
            function showRegisterDialog() {
                var registerHtml = `
                    <div style="padding: 20px;">
                        <h3 style="margin-bottom: 20px; color: #2c3e50;">用户注册</h3>
                        <form id="registerForm">
                            <div style="margin-bottom: 15px;">
                                <label style="display: block; margin-bottom: 5px; color: #495057;">用户名</label>
                                <input type="text" name="username" class="form-control" placeholder="请输入用户名" required>
                            </div>
                            <div style="margin-bottom: 15px;">
                                <label style="display: block; margin-bottom: 5px; color: #495057;">密码</label>
                                <input type="password" name="password" class="form-control" placeholder="请输入密码" required>
                            </div>
                            <div style="margin-bottom: 15px;">
                                <label style="display: block; margin-bottom: 5px; color: #495057;">确认密码</label>
                                <input type="password" name="confirmPassword" class="form-control" placeholder="请再次输入密码" required>
                            </div>
                            <div style="margin-bottom: 15px;">
                                <label style="display: block; margin-bottom: 5px; color: #495057;">真实姓名</label>
                                <input type="text" name="realName" class="form-control" placeholder="请输入真实姓名" required>
                            </div>
                            <div style="margin-bottom: 15px;">
                                <label style="display: block; margin-bottom: 5px; color: #495057;">手机号</label>
                                <input type="tel" name="phone" class="form-control" placeholder="请输入手机号" required>
                            </div>
                        </form>
                    </div>
                `;

                // 使用layer弹窗（如果可用）或原生alert
                if (typeof layer !== 'undefined') {
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 1,
                        shadeClose: true,
                        area: ['400px', '500px'],
                        content: registerHtml,
                        btn: ['注册', '取消'],
                        yes: function(index) {
                            var formData = {};
                            $('#registerForm').serializeArray().forEach(function(item) {
                                formData[item.name] = item.value;
                            });

                            if (formData.password !== formData.confirmPassword) {
                                layer.msg('两次输入的密码不一致', {icon: 2});
                                return;
                            }

                            // 发送注册请求
                            $.ajax({
                                url: '/user/register',
                                method: 'POST',
                                contentType: 'application/json',
                                data: JSON.stringify(formData),
                                success: function(res) {
                                    if (res.code === 200) {
                                        layer.msg('注册成功', {icon: 1});
                                        layer.close(index);
                                    } else {
                                        layer.msg(res.message || '注册失败', {icon: 2});
                                    }
                                },
                                error: function() {
                                    layer.msg('网络错误，请稍后重试', {icon: 2});
                                }
                            });
                        }
                    });
                } else {
                    alert('注册功能暂未开放，请联系管理员');
                }
            }

            // 回车键提交表单
            $(document).on('keypress', function(e) {
                if (e.which === 13) {
                    $('#loginForm').submit();
                }
            });
        });
    </script>
</body>
</html> 